package cn.bgs.controller;


import cn.bgs.common.AliOss;
import cn.bgs.common.R;
import cn.bgs.config.DirectHdConfig;
import cn.bgs.config.DirectXyConfig;
import cn.bgs.pojo.OfferAdvice;
import cn.bgs.pojo.PersonnelInfo;
import cn.bgs.pojo.ThematicActivities;
import cn.bgs.service.ThematicActivitiesService;
import cn.bgs.util.OrderUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;

/**
 * <p>
 * 专题活动表 前端控制器
 * </p>
 *
 * @author Mr.Zhao
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/thematic-activities")
@Slf4j
public class ThematicActivitiesController {

    @Autowired
    private ThematicActivitiesService thematicActivitiesService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Autowired
    private AliOss aliOss;

    @GetMapping("/thematicactivitiesinfo")
    @ApiOperation(value = "专题活动信息查询")
    public R thematicactivitiesinfo(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String themActivities){
        QueryWrapper<ThematicActivities> queryWrapper= new QueryWrapper<>();
        queryWrapper.like("them_activities",themActivities);
        queryWrapper.orderByDesc("create_time").eq("delete_flag",0);
        Page<ThematicActivities> page = thematicActivitiesService.page(new Page<>(pageNum, pageSize),queryWrapper);
        return R.ok().data("thematicactivitiesinfo" , page);
    }

    @RequestMapping("/deleteInfo/{id}")
    @ApiOperation(value = "活动信息删除")
    public R addInfo(@PathVariable Integer id){

        boolean save = thematicActivitiesService.removeById(id);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }


    }


    @RequestMapping("/updatedInfo")
    @ApiOperation(value = "审核信息")
    public R updatedInfo(@RequestBody ThematicActivities thematicActivities){

        try {
            rabbitTemplate.convertAndSend(DirectHdConfig.DIRECT_EXCHANGE_NAME, DirectHdConfig.DIRECT_ROUTING_KEY_NAME, thematicActivities);
            return R.ok();
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.error();
        }
    }

    @RequestMapping("/addPic")
    @ApiOperation(value = "项目图片")
    public String addPic(MultipartFile file){
        String upload = aliOss.upload(file);
        System.out.println(upload);
        return upload;
    }


    @RequestMapping("/addInfo")
    @ApiOperation(value = "反馈信息添加")
    public R addInfo(@RequestBody ThematicActivities thematicActivities){
        LocalDateTime localDateTime = LocalDateTime.now();

        thematicActivities.setThemActNumber(OrderUtils.getDateTime());
        thematicActivities.setThemActTime(localDateTime);
        thematicActivities.setThemActState("待审核");
        thematicActivities.setDeleteFlag(0);
        thematicActivities.setCreateTime(localDateTime);
        thematicActivities.setUpdateTime(localDateTime);
        boolean save = thematicActivitiesService.save(thematicActivities);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }


    }

    @GetMapping("/thematicactivitiescount")
    @ApiOperation(value = "乡镇人员注册查询")
    public R thematicactivitiescount(){
        QueryWrapper<ThematicActivities> queryWrapper= new QueryWrapper<>();

        queryWrapper.eq("delete_flag",0).gt("id",0);
        Integer tac= thematicActivitiesService.seletecount(queryWrapper);

        return R.ok().data("thematicactivitiescount" , tac);
    }


}

